package com.zx.jhpt.datamart;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.Map.Entry;

import com.zx.framework.core.env.Environment;
import com.zx.framework.core.exception.ServiceException;
import com.zx.framework.core.util.FileUtil;
import com.zx.framework.core.util.FreemarkerUtil;
import com.zx.framework.core.util.PinyinUtil;
import com.zx.framework.core.util.StringUtil;
import com.zx.framework.core.util.gson.GsonUtil;
import com.zx.jhpt.excel.Engine;
import com.zx.jhpt.excel.vo.*;
import com.zx.jhpt.service.EngineService;
import com.zx.jhpt.service.MarketEngineService;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

import javax.annotation.Resource;


/**
 * 数据集市数据处理
 *
 * @author
 */
public class MarketEngine {

    private static final Logger LOGGER = Logger.getLogger(MarketEngine.class);
    /**
     * 缓存配置对象
     */
    private static final Map<String, DataMart> map = new HashMap<String, DataMart>();

    private static MarketEngineService marketEngineService;
    /**
     * 获取所有的日志配置
     * @return
     */
    public static List<DataMart> getAll(){
    	List<DataMart> dataMarts = new ArrayList<DataMart>();
    	for (Entry<String, DataMart> entry : map.entrySet()) {
			dataMarts.add(entry.getValue());
		}
    	Collections.sort(dataMarts, new Comparator<DataMart>() {
			@Override
			public int compare(DataMart o1, DataMart o2) {
				return o1.getTableName().compareTo(o2.getTableName());
			}
		});
    	return dataMarts;
    }
    
    public static DataMart getDataMart(String tableName){
    	return map.get(tableName);
    }
    

    /**
     * 扫描log文件夹下所有符合条件的Log文件
     *
     * @return 所有符合条件的文件的文件名
     */
    public static List<String> scanLogFile() {
        List<String> logFileNames = new ArrayList<String>();
        try {
            LOGGER.debug("开始扫描" + Environment.WEB_INF_REALPATH + "log" + "文件夹下所有*.log文件...");
            Iterator<File> extensionLogFiles = FileUtils.iterateFiles(new File(Environment.WEB_INF_REALPATH + "log"), new String[]{"log"}, false);
            while (extensionLogFiles.hasNext()) {
                File logFile = extensionLogFiles.next();
                getDataMartFromFile(logFile);
                logFileNames.add(logFile.getName());
            }
        } catch (Exception e) {
            LOGGER.error("扫描日志文件出错:" + e.getMessage());
        }
        return logFileNames;
    }


    /**
     * 根据文件获取描述结构
     *
     * @param file
     * @return
     * @throws IOException
     */
    public static DataMart getDataMartFromFile(File file) throws IOException {
        String json = FileUtil.readFileToString(file, "utf-8");
        return getDataMartFromJson(json);
    }

    /**
     * 根据路径获取描述结构
     *
     * @param path
     * @return
     * @throws IOException
     */
    public static DataMart getDataMartFromPath(String path) throws IOException {
        return getDataMartFromFile(new File(path));
    }

    /**
     * 根据json获取结构
     *
     * @param json
     * @return
     */
    public static DataMart getDataMartFromJson(String json) {
        DataMart dataMart = GsonUtil.json2Bean(json, DataMart.class);
        List<com.zx.jhpt.datamart.Column> cls = new ArrayList<>();
        for (com.zx.jhpt.datamart.Column column : dataMart.getColumns()) {
            cls.add(column);
        }
        dataMart.setColumns(cls);
        putDataMart(dataMart);
        return dataMart;
    }


    /**
     * 获取生成表格的sql
     *
     * @param dataMart
     * @return
     */
    public static String createTableSql(DataMart dataMart) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("dataMart", dataMart);
        return FreemarkerUtil.getInstance().executeTemplate(map, "log/create.tpl");
    }

    /**
     * 缓存DataMart对象
     *
     * @param dataMart
     */
    public static void putDataMart(DataMart dataMart) {
        map.put(dataMart.getTableName(), dataMart);
    }
}
